#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
using tl = tuple < int, int, int >;

const int N = 2e5 + 10;

ll ts(ll a, ll b) { ll p = a / b; return p * (p - 1) / 2 * b + (a - b * p) * p; }
ll calc(ll a, ll b, ll l, ll r) { return a * (r - l) + ts(r, b) - ts(l, b); }

void solve() {
	ll n = in, a = in, b = in, c = in, d = in, l, r;
	ll ans = 0;
	if(b == d) return printf("%lld\n", a == c ? n : 0), void();
	if(b > d) swap(a, c), swap(b, d);
	l = max(b * d * (c - a - 1) / (d - b), 0ll) + 1;
	r = min(b * d * (c - a) / (d - b), n) + 1;
	if(l < r) ans = r - l - (calc(c, d, l, r) - calc(a, b, l, r));
	l = max(b * d * (c - a) / (d - b), 0ll) + 1;
	r = min(b * d * (c - a + 1) / (d - b), n) + 1;
	if(l < r) ans += r - l - (calc(a, b, l, r) - calc(c, d, l, r));
	printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	int T = in; while(T--) solve();
	return 0;
}
